import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class UpgradeRecordsService extends BaseService<any> {
  private readonly logger = new Logger(UpgradeRecordsService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * add
   * 对应 PHP: UpgradeRecordsService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data[ 'create_time' ] = time();         $res = $this->model->create($data);          // 关联备份记录         $is_need_backup = $data['is_need_backup'] ?? t...
data[ 'create_time' ] = time(];
        const res = this.model.create(data);

        // 关联备份记录
        const is_need_backup = data.is_need_backup || true;
        if (is_need_backup) {
            ( this.backupRecordsService ).add([
                version: data[ 'current_version' ],
                backup_key: data[ 'upgrade_key' ),
                content: '自动备份'
            )];
  }

  /**
   * edit
   * 对应 PHP: UpgradeRecordsService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(condition: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $this->model->where($condition)->update($data);         return true;     }      /**      * 执行完成，更新升级记录状态，备份记录状态      * @param $upgrade_key      * @ret...
this.model.where(condition).update(data);
        return true;
    }

    /**
     * 执行完成，更新升级记录状态，备份记录状态
     * @param upgrade_key
     * @return void
     */
    async complete(upgrade_key)
    {
        // 记录升级日志
        this.edit([
            [ 'upgrade_key', '=', upgrade_key )
        ), [
            status: UpgradeDict.STATUS_COMPLETE,
            complete_time: time()
        ]];
        ( this.backupRecordsService ).complete(upgrade_key);
    }

    /**
     * 执行失败，更新升级记录状态，备份记录状态
     * @param upgrade_key
     * @param fail_reason
     * @return void
     */
    async failed(upgrade_key, fail_reason = [))
    {
        this.model.where([ [ 'upgrade_key', '=', upgrade_key ) )).update([status: UpgradeDict.STATUS_FAIL, fail_reason: fail_reason )];
        ( this.backupRecordsService ).failed(upgrade_key);
    }

    /**
     * 获取升级记录信息
     * @param condition
     * @param field
     * @return */
    async getInfo(condition, field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, status, fail_reason, create_time, complete_time')
    {
        return this.model.field(field).where(condition).findOrEmpty().toArray(];
    }

    /**
     * 升级记录分页列表
     * @param where
     * @return */
    async getPage(where = [))
    {
        const field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, complete_time, create_time, status, fail_reason';
        const order = "id desc";
        const search_model = this.model.where([
//            [ 'status', '=', UpgradeDict.STATUS_COMPLETE ],
            [ 'name|content|current_version', 'like', '%' . where[ 'name' ]  + '%' )
        )).field(field).order(order).append(.status_name];
        return this.pageQuery(search_model);
    }

    /**
     * 刪除升级记录
     * @param ids
     * @return void
     */
    async del(ids) {
        this.model.where([['id', 'in', ids))).delete(;
  }
  }

  /**
   * complete
   * 对应 PHP: UpgradeRecordsService_admin::complete()
   * 逻辑类型: undefined - undefined
   */
  async complete(upgrade_key: any) {
    // 基于PHP真实逻辑: complete
    // PHP原文: // 记录升级日志         $this->edit([             [ 'upgrade_key', '=', $upgrade_key ]         ], [             'status' => UpgradeDict::STATUS_COMPLETE,   ...
// 记录升级日志
        this.edit([
            [ 'upgrade_key', '=', upgrade_key )
        ), [
            status: UpgradeDict.STATUS_COMPLETE,
            complete_time: time()
        ]];
        ( this.backupRecordsService ).complete(upgrade_key);
    }

    /**
     * 执行失败，更新升级记录状态，备份记录状态
     * @param upgrade_key
     * @param fail_reason
     * @return void
     */
    async failed(upgrade_key, fail_reason = [))
    {
        this.model.where([ [ 'upgrade_key', '=', upgrade_key ) )).update([status: UpgradeDict.STATUS_FAIL, fail_reason: fail_reason )];
        ( this.backupRecordsService ).failed(upgrade_key);
    }

    /**
     * 获取升级记录信息
     * @param condition
     * @param field
     * @return */
    async getInfo(condition, field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, status, fail_reason, create_time, complete_time')
    {
        return this.model.field(field).where(condition).findOrEmpty().toArray(];
    }

    /**
     * 升级记录分页列表
     * @param where
     * @return */
    async getPage(where = [))
    {
        const field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, complete_time, create_time, status, fail_reason';
        const order = "id desc";
        const search_model = this.model.where([
//            [ 'status', '=', UpgradeDict.STATUS_COMPLETE ],
            [ 'name|content|current_version', 'like', '%' . where[ 'name' ]  + '%' )
        )).field(field).order(order).append(.status_name];
        return this.pageQuery(search_model);
    }

    /**
     * 刪除升级记录
     * @param ids
     * @return void
     */
    async del(ids) {
        this.model.where([['id', 'in', ids))).delete(;
  }
  }

  /**
   * failed
   * 对应 PHP: UpgradeRecordsService_admin::failed()
   * 逻辑类型: undefined - undefined
   */
  async failed(upgrade_key: any[], fail_reason: any[]) {
    // 基于PHP真实逻辑: failed
    // PHP原文: $this->model->where([ [ 'upgrade_key', '=', $upgrade_key ] ])->update(['status' => UpgradeDict::STATUS_FAIL, 'fail_reason' => $fail_reason ]);        ...
this.model.where([ [ 'upgrade_key', '=', upgrade_key ) )).update([status: UpgradeDict.STATUS_FAIL, fail_reason: fail_reason )];
        ( this.backupRecordsService ).failed(upgrade_key);
    }

    /**
     * 获取升级记录信息
     * @param condition
     * @param field
     * @return */
    async getInfo(condition, field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, status, fail_reason, create_time, complete_time')
    {
        return this.model.field(field).where(condition).findOrEmpty().toArray(];
    }

    /**
     * 升级记录分页列表
     * @param where
     * @return */
    async getPage(where = [))
    {
        const field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, complete_time, create_time, status, fail_reason';
        const order = "id desc";
        const search_model = this.model.where([
//            [ 'status', '=', UpgradeDict.STATUS_COMPLETE ],
            [ 'name|content|current_version', 'like', '%' . where[ 'name' ]  + '%' )
        )).field(field).order(order).append(.status_name];
        return this.pageQuery(search_model);
    }

    /**
     * 刪除升级记录
     * @param ids
     * @return void
     */
    async del(ids) {
        this.model.where([['id', 'in', ids))).delete(;
  }
  }

  /**
   * getInfo
   * 对应 PHP: UpgradeRecordsService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(condition: any[], field: any[]) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: return $this->model->field($field)->where($condition)->findOrEmpty()->toArray();     }      /**      * 升级记录分页列表      * @param array $where      * @ret...
return this.model.field(field).where(condition).findOrEmpty().toArray(];
    }

    /**
     * 升级记录分页列表
     * @param where
     * @return */
    async getPage(where = [))
    {
        const field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, complete_time, create_time, status, fail_reason';
        const order = "id desc";
        const search_model = this.model.where([
//            [ 'status', '=', UpgradeDict.STATUS_COMPLETE ],
            [ 'name|content|current_version', 'like', '%' . where[ 'name' ]  + '%' )
        )).field(field).order(order).append(.status_name];
        return this.pageQuery(search_model);
    }

    /**
     * 刪除升级记录
     * @param ids
     * @return void
     */
    async del(ids) {
        this.model.where([['id', 'in', ids))).delete(;
  }
  }

  /**
   * getPage
   * 对应 PHP: UpgradeRecordsService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, complete_time, create_time, status, fail_reason';         $order = "...
const field = 'id, upgrade_key, app_key, name, content, prev_version, current_version, complete_time, create_time, status, fail_reason';
        const order = "id desc";
        const search_model = this.model.where([
//            [ 'status', '=', UpgradeDict.STATUS_COMPLETE ],
            [ 'name|content|current_version', 'like', '%' . where[ 'name' ]  + '%' )
        )).field(field).order(order).append(.status_name];
        return this.pageQuery(search_model);
    }

    /**
     * 刪除升级记录
     * @param ids
     * @return void
     */
    async del(ids) {
        this.model.where([['id', 'in', ids))).delete(;
  }
  }

  /**
   * del
   * 对应 PHP: UpgradeRecordsService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(ids: any) {
    // 基于PHP真实逻辑: del
    // PHP原文: $this->model->where([['id', 'in', $ids]])->delete();     } }...
this.model.where([['id', 'in', ids))).delete(;
  }
  }
}
